#ifndef _zgn_H
#define _zgn_H

//4x4键盘驱动     在setup中调用jianpan_init()后   在循环中调用get_jian()，此函数返回按下的键值  第一行第一个为1    的一行第二个为2 以此类推


#define R1 26//第一行
#define R2 25
#define R3 33
#define R4 32//第四行
#define c1 27//第一列
#define c2 14
#define c3 12
#define c4 13//第四列


void jianpan_init(){
     pinMode(R1, OUTPUT);
     pinMode(R2, OUTPUT);
     pinMode(R3, OUTPUT);
     pinMode(R4, OUTPUT);

     pinMode(c1, INPUT);
     pinMode(c2, INPUT);
     pinMode(c3, INPUT);
     pinMode(c4, INPUT);
 
}

int an_jian_zhi()
{
   int jianzhi = 0;

   digitalWrite(R1,HIGH);
   digitalWrite(R2,LOW);
   digitalWrite(R3,LOW);
   digitalWrite(R4,LOW);
   if(digitalRead(c1)==1){delay(15);if(digitalRead(c1)==1)jianzhi=1;}
   if(digitalRead(c2)==1){delay(15);if(digitalRead(c2)==1)jianzhi=2;}
   if(digitalRead(c3)==1){delay(15);if(digitalRead(c3)==1)jianzhi=3;}
   if(digitalRead(c4)==1){delay(15);if(digitalRead(c4)==1)jianzhi=4;}
   if(jianzhi>0){goto go;} 
   
   

   digitalWrite(R1,LOW);
   digitalWrite(R2,HIGH);
   digitalWrite(R3,LOW);
   digitalWrite(R4,LOW);
   if(digitalRead(c1)==1){delay(15);if(digitalRead(c1)==1)jianzhi=5;}
   if(digitalRead(c2)==1){delay(15);if(digitalRead(c2)==1)jianzhi=6;}
   if(digitalRead(c3)==1){delay(15);if(digitalRead(c3)==1)jianzhi=7;}
   if(digitalRead(c4)==1){delay(15);if(digitalRead(c4)==1)jianzhi=8;}
   if(jianzhi>0){goto go;} 


   digitalWrite(R1,LOW);
   digitalWrite(R2,LOW);
   digitalWrite(R3,HIGH);
   digitalWrite(R4,LOW);
   if(digitalRead(c1)==1){delay(15);if(digitalRead(c1)==1)jianzhi=9;}
   if(digitalRead(c2)==1){delay(15);if(digitalRead(c2)==1)jianzhi=10;}
   if(digitalRead(c3)==1){delay(15);if(digitalRead(c3)==1)jianzhi=11;}
   if(digitalRead(c4)==1){delay(15);if(digitalRead(c4)==1)jianzhi=12;}
   if(jianzhi>0){goto go;} 

   
   digitalWrite(R1,LOW);
   digitalWrite(R2,LOW);
   digitalWrite(R3,LOW);
   digitalWrite(R4,HIGH);
   if(digitalRead(c1)==1){delay(15);if(digitalRead(c1)==1)jianzhi=13;}
   if(digitalRead(c2)==1){delay(15);if(digitalRead(c2)==1)jianzhi=14;}
   if(digitalRead(c3)==1){delay(15);if(digitalRead(c3)==1)jianzhi=15;}
   if(digitalRead(c4)==1){delay(15);if(digitalRead(c4)==1)jianzhi=16;}
   if(jianzhi>0){goto go;} 


   go:return jianzhi;
   
}



/*

// 返回0表示未被按下    返回键值表示短按     返回键值乘以100表示长按        上机结果未实现
int get_jian(){
  bool xia_jiang_yan = false;
  int jian = an_jian_zhi();
  int anjian = 0;
  if(jian==0){
    delay(10);
   if(an_jian_zhi()){
       xia_jiang_yan = true;
    }
   }
  if(xia_jiang_yan){
    int i=0;
   for(i;0<an_jian_zhi()<17;i++){
      delay(10);
  }
    if(100>i>0){anjian = an_jian_zhi();}
    else if(i>=100){anjian= an_jian_zhi()*100;}
  }
return an_jian_zhi();

}

*/

int get_jian()
{
int jian_zhi; 
int zhi = an_jian_zhi();
delay(20);
if(zhi==an_jian_zhi()){   jian_zhi = zhi;   }
  return jian_zhi;
}



// 返回0表示未被按下    返回键值表示短按     返回键值乘以100表示长按   上机结果实现    但是如果上一次是短按结果下一次长按松开的一瞬间显示的是短按结果
int get_cang_an(){

int i = 0;    
int jian_zhi; 
int zhi = an_jian_zhi();
delay(20);
if(zhi==an_jian_zhi()){  while(an_jian_zhi()){   delay(5);   i+=1;   if(i>100){goto go1;}    }  jian_zhi= zhi; go1: if(i>100){jian_zhi=zhi*100;}              }
  return jian_zhi;
}


#endif //

